package com.rzico.sbl.ui.report.dispatch

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import com.lzg.extend.jackson.JsonUtil
import com.rzico.sbl.R
import com.rzico.sbl.databinding.FragmentDispatchSecondBinding
import com.rzico.sbl.model.DispatchPay
import com.rzico.sbl.viewmodel.ReportDispatchViewModel
import com.xinnuo.common.extend.formatDecimal
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.goneValue
import com.xinnuo.common.extend.visibleValue
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common.helper.doubleValue
import com.xinnuo.common.helper.intValue
import com.xinnuo.common_ui.base.NavigationFragment
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.bindLifeCycle
import com.xinnuo.common_ui.utils.subscribeBySuccess
import com.xinnuo.slimadapter.SlimAdapter
import org.json.JSONArray
import org.json.JSONObject

class DispatchSecondFragment
@JvmOverloads
constructor(
    private val shippingSn: String = ""
) : NavigationFragment() {

    private var mBinding: FragmentDispatchSecondBinding? = null

    override fun getViewModel() = get<ReportDispatchViewModel>()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = FragmentDispatchSecondBinding
        .inflate(inflater, container, false)
        .also { mBinding = it }
        .root

    override fun initTitle() {
        initLayout()

        mBinding?.let {
            it.listLayout
                .swipeRefresh
                .isRefreshing = true

            getData()
        }
    }

    private fun initLayout() {
        mBinding?.let {
            it.listLayout.apply {
                recycleList.also { layout ->
                    layout.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData() }
                    )

                    layout.addItemDecoration(SpaceTBDecoration(hasHeader = false))

                    mAdapter = SlimAdapter.creator()
                        .register<DispatchPay>(R.layout.item_report_dispatch_pay) { jector, bean, _ ->
                            jector.apply {
                                gone(R.id.item_pay_edit_ll)
                                text(R.id.item_pay_barrel, bean.name)
                                text(R.id.item_pay_out, bean.quantity)
                                text(R.id.item_pay_price, bean.price.formatDecimal())
                                text(R.id.item_pay_memo, bean.memo1)
                                // 免单
                                text(R.id.item_pay_free, bean.free)
                                // 漏桶
                                text(R.id.item_pay_omit, bean.swap)
                                // 水票
                                text(R.id.item_pay_ticket, bean.exchange)
                                text(R.id.item_pay_amount, bean.exchangePrice)
                                // 优惠券
                                text(R.id.item_pay_coupon, bean.coupon.formatDecimal())
                                // 小计
                                text(R.id.item_pay_total, bean.memo)

                                visibility(
                                    R.id.item_pay_ticket_ll,
                                    visibleValue { bean.exchange.intValue > 0 }
                                )

                                visibility(
                                    R.id.item_pay_coupon_ll,
                                    visibleValue { bean.coupon.doubleValue > 0 }
                                )

                                visibility(
                                    R.id.item_pay_memo,
                                    goneValue { bean.memo1.isNullOrEmpty() }
                                )
                            }
                        }
                        .attachTo(layout)
                }
            }
        }
    }

    /**
     * 收款列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .dispatchPay(
                shippingSn = shippingSn,
                isLoading = false
            )
            .bindLifeCycle(getLifecycleOwner())
            .subscribeBySuccess(
                event = getViewModel(),
                onSuccess = { result ->
                    val obj = JSONObject(result)

                    val objItems = obj.optJSONArray("shippingItems") ?: JSONArray()
                    mList.clear()
                    mList.addItems(
                        JsonUtil.toList(
                            objItems.toString(),
                            DispatchPay::class.java
                        )
                    )

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    mBinding?.let {
                        it.listLayout
                            .swipeRefresh
                            .isRefreshing = false
                    }
                }
            )
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mBinding = null
    }

    companion object {
        fun newInstance(shippingSn: String) = DispatchSecondFragment(shippingSn)
    }

}